package com.kc.demo.kcapidemo.demo;

import com.kc.demo.kcapidemo.utils.HttpClientUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.kc.demo.kcapidemo.demo
 * @类名称：ExcelPoiDemo
 * @创建人：YIHANG.XU
 * @创建时间：2021/10/91:00 下午
 * @修改人：YIHANG.XU
 * @修改时间：2021/10/91:00 下午
 * @修改备注：
 * @Copyright
 */
public class ExcelPoiDemo {
    public static void main(String[] args) throws Exception {
        String filePath = "/Users/xuyihang/Desktop/多次违法车辆09259xls.xls";
        InputStream inputStream = new FileInputStream(filePath);
        Workbook workbook = new HSSFWorkbook(inputStream);
        List<CarVo> list = new ArrayList<>();
        if (workbook == null) {
            return;
        } else {
            //获取所有的工作表的的数量
            int numOfSheet = workbook.getNumberOfSheets();

            //遍历表
            for (int i = 0; i < numOfSheet; i++) {
                //获取一个sheet也就是一个工作本。
                Sheet sheet = workbook.getSheetAt(i);
                if (sheet == null) continue;
                //获取一个sheet有多少Row
                int lastRowNum = sheet.getLastRowNum();
                if (lastRowNum == 0) continue;
                System.out.println(lastRowNum);
                Row row;
                for (int j = 1; j <= lastRowNum; j++) {
                    row = sheet.getRow(j);
                    if (row == null) {
                        continue;
                    }
                    String key = row.getCell(1).getStringCellValue().trim();
                    String name = row.getCell(4).getStringCellValue().trim();
                    CarVo carVo = new CarVo();
                    carVo.setCarId("浙"+key);
                    if (!StringUtils.isEmpty(name)) {
                        carVo.setCarName(name);
                    } else {
                        carVo.setCarName("未知");
                    }
                    carVo.setCarSize("2");
                    list.add(carVo);

                }
            }

           saveCar(list);
        }

    }
   public static void saveCar(List<CarVo> list){
       RestTemplate sllRestTemplate = null;
       try {
           sllRestTemplate = HttpClientUtil.getSllRestTemplate();
       } catch (Exception e) {
           e.printStackTrace();
       }
       KcHelmetDemo.login(sllRestTemplate);
       for (CarVo carVo : list) {
           carDb(sllRestTemplate,carVo);
       }

   }
    public static void carDb(RestTemplate restTemplate, CarVo carVo) {
        String dbUrl = "https://33.192.75.46:5558/helmetBusiness/api/licensePlate/save";
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
        form.add("licensePlate", carVo.getCarId());
        form.add("vehicleOwner", carVo.getCarName());
        form.add("ownerPhone", "");
        form.add("vehicleType", "2");
        form.add("dbId", 6);
        form.add("lang", "zh_CN");

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(form, headers);
        ResponseEntity<String> exchange = restTemplate.exchange(dbUrl, HttpMethod.POST, httpEntity, String.class);
        String body = exchange.getBody();
        System.err.println(body);

    }
}
